package com.zoshine.zzg.service;




import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zoshine.zzg.dao.PrisonerAccountDao;
import com.zoshine.zzg.dao.PrisonerCapitalFlowDao;
import com.zoshine.zzg.dao.PrisonerInfoDao;
import com.zoshine.zzg.pojo.PrisonerAccount;
import com.zoshine.zzg.pojo.PrisonerCapitalFlow;
import com.zoshine.zzg.pojo.PrisonerInfo;
import com.zoshine.zzg.pojo.httpclient.ApplyPayment;
import com.zoshine.zzg.pojo.httpclient.ApplyAccount;
import com.zoshine.zzg.pojo.pojostatus.PrisonerAccountStatus;
import com.zoshine.zzg.pojo.pojostatus.PrisonerCapitalFlowStatus;
import com.zoshine.zzg.pojo.pojostatus.PrisonerInfoBankStatus;
import com.zoshine.zzg.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * 调农行接口处理
 * @author mazefei
 * @time 2017年11月8日
 */

 @Service
 //@Transactional
public class BankService {


    private PrisonerInfoDao prisonerInfoDao;
    private PrisonerAccountDao prisonerAccountDao;
    private PrisonerCapitalFlowDao prisonerCapitalFlowDao;

    @Autowired
    public void setPrisonerInfoDao(PrisonerInfoDao prisonerInfoDao) {
        this.prisonerInfoDao = prisonerInfoDao;
    }

    @Autowired
    public void setPrisonerAccountDao(PrisonerAccountDao prisonerAccountDao) {
        this.prisonerAccountDao = prisonerAccountDao;
    }

    @Autowired
    public void setPrisonerCapitalFlowDao(PrisonerCapitalFlowDao prisonerCapitalFlowDao) {
        this.prisonerCapitalFlowDao = prisonerCapitalFlowDao;
    }

    /**
     * 调农行接口
     * 犯人开户申请
     * @param
     * @return
     */

    public  void applyAccount() {

        //开户 提交开户信息
        PrisonerInfo prisonerInfo = new PrisonerInfo();
        prisonerInfo.setBankStatus(PrisonerInfoBankStatus.FINISH_BANK.getValue());
        List<PrisonerInfo> listPrisonerInfo = prisonerInfoDao.selectList(prisonerInfo);
        List<Object> prisonerlist = new ArrayList<Object>();
        String url = "http://10.120.131.19:8080/b_api/jyjgj/addNew";
        for (PrisonerInfo temp : listPrisonerInfo) {
            ApplyAccount applyAccount = new ApplyAccount();
            //犯人编号
            applyAccount.setPrisonerno(String.valueOf(temp.getId()));
            //犯人姓名
            applyAccount.setName(temp.getName());
            //身份证
            applyAccount.setIdno(temp.getIdCard());
            //监区号
            applyAccount.setDepartment(temp.getArea());
            applyAccount.setNation("");
            applyAccount.setSex("");
            applyAccount.setIndate("");
            //暂时为空
            applyAccount.setLevel("");
            applyAccount.setAlm("0");
            applyAccount.setBlm("0");
            prisonerlist.add(applyAccount);
        }
        JSONObject json = new JSONObject();
        json.put("prisonerList", prisonerlist);
        JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url, json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        //申请成功 10 已经申请 01 出错,申请数据为空09
        if (result.get("result").equals("10")) {
            for (PrisonerInfo temp : listPrisonerInfo) {
                //修改状态为 开户处理中
                temp.setBankStatus(PrisonerInfoBankStatus.HANDLING.getValue());
                prisonerInfoDao.update(temp);
            }
        }


    }


    /**
     * 调农行接口
     * 犯人开户情况查询
     * @param
     * @return
     */
    public void queryAccount() {

        //犯人开户情况查询
        String url = null;
        url = "http://10.120.131.19:8080/b_api/jyjgj/cardInfo";
        PrisonerInfo prisonerInfo = new PrisonerInfo();
        prisonerInfo.setBankStatus(PrisonerInfoBankStatus.HANDLING.getValue());
        List<PrisonerInfo> listPrisonerInfo = prisonerInfoDao.selectList(prisonerInfo);
        StringBuffer buffer = new StringBuffer();
        for (PrisonerInfo temp : listPrisonerInfo) {
            buffer.append("'").append(temp.getId()).append("',");
        }
        String keyid = buffer.toString();
        keyid = keyid.substring(0,keyid.length() - 1);
        JSONObject json = new JSONObject();
        json.put("department","");
        //犯人编号
        json.put("keyid",keyid );
        json.put("startdate","");
        json.put("enddate","");
        JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);

        JSONArray jsonArray = (JSONArray) result.get("cardInfo");
        for(int i = 0;i < jsonArray.size();i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            System.out.println( obj.get("result"));
            //  0000 开户成功 9999 还未处理开卡
            // 农行这边说没有开不出卡这种情况
            if(obj.get("result").equals("0000")){
                //修改个人信息状态，开户成功，待发送内网
                String a = (String) obj.get("prisonerno");
                PrisonerInfo temp = prisonerInfoDao.selectByPrimaryKey(Long.parseLong(a) );
                temp.setBankStatus(PrisonerInfoBankStatus.SUCCESS_NOBACK.getValue());
                prisonerInfoDao.update(temp);
                //生成账户信息
                PrisonerAccount prisonerAccount = new PrisonerAccount();
                prisonerAccount.setId(Long.parseLong(String.valueOf(obj.get("prisonerno"))) );
                //状态
                prisonerAccount.setStatus(PrisonerAccountStatus.SUCCESS_NOBACK.getValue());
                //账户
                prisonerAccount.setBankCard((String) obj.get("djaccount"));
                //账户余额
                prisonerAccount.setBalance(myMath(obj.get("balance")));
                //账户金额
                prisonerAccount.setTotalBalance(myMath(obj.get("balance")));
                //abc账户余额
                prisonerAccount.setaLevel(myMath(obj.get("abalance")));
                prisonerAccount.setbLevel(myMath(obj.get("bbalance")));
                prisonerAccount.setcLevel(myMath(obj.get("cbalance")));
                //限额
                prisonerAccount.setConsumptionThreshold(myMath(obj.get("icabalance")));
                prisonerAccount.setBankName("中国农业银行");
                prisonerAccountDao.insert(prisonerAccount);

            }
            if(obj.get("result").equals("9999")){
                //修改个人信息状态，开户失败，待发送内网
                /*String a = (String) obj.get("prisonerno");
                PrisonerInfo temp = prisonerInfoDao.selectByPrimaryKey(Long.parseLong(a));
                temp.setStatus(PrisonerInfoBankStatus.FAIL_NOBACK.getValue());
                prisonerInfoDao.update(temp);*/
                //生成账户信息
            }

        }

    }

    /**
     * 调农行接口
     * 犯人扣款申请
     * @param
     * @return
     */
     public  void applyPayment(){
        String url = "http://10.120.131.19:8080/b_api/jyjgj/addOrder";
        PrisonerCapitalFlow prisonerCapitalFlow = new PrisonerCapitalFlow();
        prisonerCapitalFlow.setStatus(PrisonerCapitalFlowStatus.FINISH_BANK.getValue());
        List<PrisonerCapitalFlow> lisPrisonerCapitalFlow = prisonerCapitalFlowDao.selectList(prisonerCapitalFlow);
         List<Object> applyPaymentList = new ArrayList<Object>();
        for (PrisonerCapitalFlow temp:lisPrisonerCapitalFlow){
            ApplyPayment applyPayment = new ApplyPayment();
            applyPayment.setName(temp.getPrisonerName());
            //犯人编号
            applyPayment.setPrisonerno(String.valueOf(temp.getPrisonerId()));
            //流水号
            applyPayment.setPriseqno(String.valueOf(temp.getId()));
            //扣款金额 除100
            applyPayment.setAmount(String.valueOf(new BigDecimal(temp.getFlowMoney()).divide(new BigDecimal(100))));
            //扣款类型
            //applyPayment.setType(String.valueOf(temp.getFlowType()));
            applyPayment.setType("101");
            applyPayment.setDepartment("");
            applyPayment.setAls("");
            applyPayment.setBls("");
            applyPayment.setIndate("");
            applyPaymentList.add(applyPayment);
        }
         JSONObject json = new JSONObject();
         json.put("orderList", applyPaymentList);
         JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
         System.out.println(result);
         //申请成功 10 已经申请 01 出错,申请数据为空09
        if(result.get("result").equals("10")){
            for (PrisonerCapitalFlow temp:lisPrisonerCapitalFlow){
                //修改状态为 扣款处理中
                temp.setStatus(PrisonerCapitalFlowStatus.HANDLING.getValue());
                prisonerCapitalFlowDao.update(temp);
            }
        }

    }



    /**
     * 调农行接口
     * 犯人扣款情况查询
     * @param
     * @return
     */
    public void queryPayment(){
        //犯人扣款情况查询
        String url = "http://10.120.131.19:8080/b_api/jyjgj/transInfo";
        PrisonerCapitalFlow prisonerCapitalFlow = new PrisonerCapitalFlow();
        prisonerCapitalFlow.setStatus(PrisonerCapitalFlowStatus.HANDLING.getValue());
        List<PrisonerCapitalFlow> lisPrisonerCapitalFlow = prisonerCapitalFlowDao.selectList(prisonerCapitalFlow);
        StringBuffer buffer = new StringBuffer();
        for (PrisonerCapitalFlow temp:lisPrisonerCapitalFlow){
            buffer.append("'").append(temp.getId()).append("',");
        }
        String keyid = buffer.toString();
        keyid = keyid.substring(0,keyid.length() - 1);
        JSONObject json = new JSONObject();
        json.put("department","");
        //流水号
        json.put("keyid",keyid );
        json.put("startdate","");
        json.put("enddate","");
        JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url,json);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(result);

        JSONArray jsonArray = (JSONArray) result.get("transInfo");
        for(int i = 0;i < jsonArray.size();i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            System.out.println( obj.get("result"));
            //修改流水状态，扣款成功，待发送内网
            //  0000 开户成功 9999 还未处理扣款 7777其他异常
            if(obj.get("result").equals("0000")){
                PrisonerCapitalFlow temp = prisonerCapitalFlowDao.selectByPrimaryKey(Long.parseLong((String) obj.get("priseqno")));
                temp.setStatus(PrisonerCapitalFlowStatus.SUCCESS_DELIVERY_INNER.getValue());
                prisonerCapitalFlowDao.update(temp);
             //修改流水状态，扣款失败，待发送内网
            }
            if(obj.get("result").equals("7777")) {
                PrisonerCapitalFlow temp = prisonerCapitalFlowDao.selectByPrimaryKey(Long.parseLong((String) obj.get("priseqno")));
                temp.setStatus(PrisonerCapitalFlowStatus.FAIL_DELIVERY_INNER.getValue());
                prisonerCapitalFlowDao.update(temp);
            }

        }
    }


    /**
     * 调农行接口
     * 犯人余额查询
     * @param
     * @return
     */
    public void queryBalance(){
        //犯人余额查询
        String url = "http://10.120.131.19:8080/b_api/jyjgj/balInfo";
        JSONObject json = new JSONObject();
        json.put("department","");
        //犯人ID为空 查全部余额
        json.put("keyid","" );
        json.put("startdate","");
        json.put("enddate","");
        JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        System.out.println(result.get("result"));
        System.out.println(result.get("resultmsg"));
        JSONArray jsonArray = (JSONArray) result.get("balInfo");
        for(int i = 0;i < jsonArray.size();i++){
            JSONObject obj = jsonArray.getJSONObject(i);
            System.out.println( obj);
            //本地账户不为null，更新账户余额
            PrisonerAccount prisonerAccount = prisonerAccountDao.selectByPrimaryKey(Long.parseLong(String.valueOf(obj.get("prisonerno"))));
            if(null != prisonerAccount){
                //账户余额
                prisonerAccount.setBalance(myMath(obj.get("balance")));
                //账户金额
                prisonerAccount.setTotalBalance(myMath(obj.get("balance")));
                prisonerAccountDao.insert(prisonerAccount);
            }


        }
    }


    /**
     * 调农行接口
     * 监区表查询
     * @param
     * @return
     */
    public void queryDepartment(){
        String url = "http://10.120.131.19:8080/b_api/jyjgj/departmentInfo";
        JSONObject json = new JSONObject();
        JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        System.out.println(result.get("result"));
        System.out.println(result.get("resultmsg"));
        JSONArray jsonArray = (JSONArray) result.get("departmentInfo");
        for(int i = 0;i < jsonArray.size();i++){
            JSONObject obj2 = jsonArray.getJSONObject(i);
            System.out.println(obj2);
        }
    }

    /**
     * 调农行接口
     * 消费科目查询
     * @param
     * @return
     */
    public void queryType(){
        String url = "http://10.120.131.19:8080/b_api/jyjgj/capilistInfo";
        JSONObject json = new JSONObject();
        JSONObject result = null;
        try {
            result = HttpClientUtils.httpPost(url,json);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(result);
        System.out.println(result.get("result"));
        System.out.println(result.get("resultmsg"));
        JSONArray jsonArray = (JSONArray) result.get("capilistInfo");
        for(int i = 0;i < jsonArray.size();i++){
            JSONObject obj2 = jsonArray.getJSONObject(i);
            System.out.println( obj2);
        }
    }

    private Integer myMath(Object obj){
        BigDecimal account = new BigDecimal((String.valueOf(obj))).multiply(new BigDecimal(100));
        return account.intValue();
    }
}
